/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

#include <ESP8266WiFi.h>
#include <SimpleDHT.h>//导入新安装的库
#include <Servo.h>

Servo myservo;  // 创建一个myervo对象，名字想怎么取怎么取
int jiaodu = 0;//初始角度


int pinDHT11 = 2;//同时使用GPIO引脚的编号声明一个全局变量，以便于更改。在这种情况下，我们将使用GPIO2
SimpleDHT11 dht11(pinDHT11);

#ifndef STASSID
#define STASSID "TP-LINK_604"
#define STAPSK  "ssj1234554321"
#endif

const char* ssid     = STASSID;
const char* password = STAPSK;

const char* host = "122.51.46.108";
const uint16_t port = 8848;
// Use WiFiClient class to create TCP connections
WiFiClient client;

void setup() {
  myservo.attach(4);  // 把引脚4设定为控制引脚，对应的esp8266引脚为D2
  myservo.write(0); // 初始化为0
  Serial.begin(115200);

  // 雨滴传感器
  pinMode(A0,INPUT);

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }
}

bool isOpen = false; // 雨棚是否开启
int noRainTimes = 0; // 雨棚开启时, 低于指定值后记数
void loop() {
  byte temperature = 0;
  byte humidity = 0;//在你的主循环中声明两个字节变量，一个用于温度，另一个用于湿度：
 int err = SimpleDHTErrSuccess;
 if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
   Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
   return;
 }
 
 Serial.print((int)temperature); Serial.print(" *C, "); 
 Serial.print((int)humidity); Serial.println(" H");

  // 雨滴传感器相关
  int currentValue = (map(analogRead(A0), 0, 1023, 235, 0));
  Serial.println((map(analogRead(A0), 0, 1023, 235, 0)));
  int pos;
  if (currentValue > 10 && !isOpen) {
    isOpen = true;
    Serial.println("testtest");
    myservo.write(180);
    noRainTimes = 0;
  } else if (currentValue < 10 && isOpen) {
    if (noRainTimes == 10) { // 原逻辑是60 现在改成10更好的测试
      isOpen = false;
      myservo.write(0);
      noRainTimes = 0;
    } else {
      noRainTimes++;
    }
  }
  // This will send a string to the server
  Serial.println(noRainTimes);
  if (client.connected()) {
    String sendData = (String)"{\"tem\":\"" + temperature + "\",\"hum\":\"" + humidity + "\",\"isOpen\":\"" + isOpen + "\"}";
    client.print(sendData);
  } else {
    Serial.print("connecting to ");
    Serial.print(host);
    Serial.print(':');
    Serial.println(port);
    if (!client.connect(host, port)) {
      Serial.println("connection failed");
      delay(5000);
      return;
    }
  }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  delay(3000); // execute once every 5 minutes, don't flood remote service
}